package com.teng.mylibrary.ui.activity

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.google.gson.Gson
import com.teng.mylibrary.base.BaseViewModel
import com.teng.mylibrary.callback.livedata.event.EventLiveData
import com.teng.mylibrary.util.SPUtil

/**
 * Description:
 * Author:       liuteng
 * CreateDate:   2022/7/7 9:47
 * UpdateRemark: 更新说明
 */
class SystemConfigVModel : BaseViewModel() {

    val ipList = ObservableArrayList<String>()
    val timeoutTime = ObservableField<String>("")
    val doExitAble = ObservableField(false)
    val exitTime = ObservableField<String>("")
    val myUc by lazy { UIChangeObservable() }

    inner class UIChangeObservable {
        val saveNotify = EventLiveData<Void>()
    }

    override fun initData() {
        super.initData()
    }


    fun save() {
        when {
            timeoutTime.get().isNullOrEmpty() || timeoutTime.get()!!
                .toDouble() < 10 -> {
                uc.showProTextEvent.value = "超时时间不能小于10秒"
            }
            doExitAble.get() == true && (exitTime.get().isNullOrEmpty() || exitTime.get()!!
                .toInt() == 0) -> uc.showProTextEvent.value =
                "请输入无操作退出时长"
            else -> {
                SPUtil.setIpList(Gson().toJson(ipList))
                SPUtil.setConnectTimeOut(timeoutTime.get().toString())
                SPUtil.setDoExit(doExitAble.get() == true)
                SPUtil.setDoExitTime(exitTime.get().toString())
                myUc.saveNotify.call()
            }
        }

    }


}